package com.zhongwang.mms.module.tool.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.module.tool.model.AccOffEquipmentModel;
import com.zhongwang.mms.module.tool.model.AccOffMaterModel;
import com.zhongwang.mms.module.tool.model.ReportAccOffEquipmentModel;
import com.zhongwang.mms.module.tool.model.ReportAccOffEquipmentModel;
import com.zhongwang.mms.module.tool.service.AccOffEquipmentService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 办公设备台账
 */
@Controller
public class OffEquipmentController  extends BaseController{

    @Autowired
    private AccOffEquipmentService offEquipmentService;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/tool/offEquipment/page")
    @RequiresPermissions({"drive:officesb:view"})
    public String linkePage() {
       return "tool/offEquipment/list";
    }

    /**
     * 查询设备台账信息
     *
     * @param model 设备台账参数
     * @return EquipmentModel
     */
    @ResponseBody
    @PostMapping("/tool/offEquipment/list")
    @RequiresPermissions({"drive:officesb:view"})
    public JsonResult selectAccEquipmentList(ReportAccOffEquipmentModel model) {
        startPage();
        return jsonResult(offEquipmentService.selectAccEquipmentList(model));
    }

    /**
     * 办公设备台账-> 导入
     *
     * @param file      导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/tool/offEquipment/import")
    @RequiresPermissions({"drive:officesb:add"})
    public JsonResult dailyTotalImport(@RequestParam("file") MultipartFile file) {
        return success(offEquipmentService.importFile(file));
    }

    /**
     * 删除
     * @param empId 主键id
     * @return
     */
    @PostMapping("/tool/offEquipment/del")
    @ResponseBody
    @RequiresPermissions({"drive:officesb:del"})
    public JsonResult deleteByPrimaryKey(Integer empId){
        JsonResult jsonResult = new JsonResult();
        Integer flg=offEquipmentService.del(empId);
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }

    /**
     * 新增设备台账信息
     */
    @PostMapping("/tool/offEquipment/add")
    @ResponseBody
    @RequiresPermissions({"drive:officesb:add"})
    public JsonResult insert(ReportAccOffEquipmentModel data){
        JsonResult jsonResult = new JsonResult();
        int flg=-1;
        /**
         * 设置人员信息和删除标识
         */
        data.setOffId(0);
        ReportAccOffEquipmentModel keyModel=new ReportAccOffEquipmentModel();
        //根据厂内编号查询
        keyModel.setOffEqCode(data.getOffEqCode());
        ReportAccOffEquipmentModel checkModel=offEquipmentService.selectAccOffEquipment(keyModel);
        if(checkModel!= null){
            //更新参数的model并且赋值
            data.setOffId(checkModel.getOffId());

            //执行修改，不存在序号的问题。
            data.setOffUpdUser(ShiroUtils.getUser().getName());
            flg=offEquipmentService.update(data);
        }else {
            if(data.getOffSortNum()==null) {
                //需要查询最大的序号，间隔为10。
                data.setOffSortNum(offEquipmentService.selectMaxNum());
            }
            data.setOffCreUser(ShiroUtils.getUser().getName());
            data.setOffUpdUser(ShiroUtils.getUser().getName());
            data.setOffIsdelete(0);
            flg= offEquipmentService.insert(data);
        }
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }



    /**
     * 编辑设备台账信息
     */
    @PostMapping("/tool/offEquipment/edit")
    @ResponseBody
    @RequiresPermissions({"drive:officesb:edit"})
    public JsonResult edit(ReportAccOffEquipmentModel data){
        JsonResult jsonResult = new JsonResult();
        data.setOffUpdUser(ShiroUtils.getUser().getName());
        int flg= offEquipmentService.update(data);
        if(flg==1){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    /**
     * 检查同一主键是否存在
     * @param paramModel 类型名称和id
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/tool/offEquipment/checkUnique")
    public ValidResult checkUnique(AccOffEquipmentModel paramModel) {
        List<AccOffEquipmentModel> list = offEquipmentService.findByCode(paramModel);
        if(list.size()>0) {
            //已经存在了。
            return ValidResult.FAIL;
        }
        return ValidResult.SUCCESS;
    }
    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/tool/offEquipment/exist")
    public JsonResult exist(String url) {
        return jsonResult(offEquipmentService.exist(url));
    }
}
